package com.king.base.rest.email;
import com.king.base.aop.Log;
import com.king.base.domain.email.EmailConfig;
import com.king.base.domain.menu.vo.EmailVo;
import com.king.base.service.email.EmailService;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 发送邮件
 * @author TianYZ
 * @date 2019-06-16
 */
@RestController
@RequestMapping("api/tools/email")
@Api(value = "email",description = "邮件发送Controller")
public class EmailController {

    private static final Logger log=LoggerFactory.getLogger(EmailController.class);
    @Autowired
    private EmailService emailService;

    @GetMapping(value = "/get")
    public ResponseEntity get(){
        return new ResponseEntity(emailService.find(),HttpStatus.OK);
    }

    @Log(description = "配置邮件")
    @PutMapping(value = "/emailConfig")
    public ResponseEntity emailConfig(@Validated @RequestBody EmailConfig emailConfig){
        emailService.update(emailConfig,emailService.find());
        return new ResponseEntity(HttpStatus.OK);
    }

    @Log(description = "发送邮件")
    @PostMapping(value = "/send")
    public ResponseEntity send(@Validated @RequestBody EmailVo emailVo) throws Exception {
        log.warn("REST request to send Email : {}" +emailVo);
        emailService.send(emailVo,emailService.find());
        return new ResponseEntity(HttpStatus.OK);
    }
}
